rm(list = ls())
require(stargazer)
require(mfx)
require(msm)
require(nnet)

#Load in some Data
data2=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/columbia_experiment_may11_type2clean.csv", header=TRUE)
data3=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/columbia_experiment_may11_type3clean.csv", header=TRUE)
data4=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/columbia_experiment_may11_type4clean.csv", header=TRUE)
data5=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/columbia_experiment_may11_type5clean.csv", header=TRUE)

data16=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/Columbia_experiment_September27_type16clean.csv", header=TRUE)
data17=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/Columbia_experiment_September27_type17clean.csv", header=TRUE)
data18=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/Columbia_experiment_September27_type18clean.csv", header=TRUE)
data19=read.csv("C:/Users/neligh/Dropbox (Chapman)/Dean Stuff/data/Columbia_experiment_September27_type19clean.csv", header=TRUE)

dataframe16=data.frame(data16)
dataframe16=subset(dataframe16,uid>1600)
dataframe17=data.frame(data17)
dataframe17=subset(dataframe17,uid>1700)
dataframe18=data.frame(data18)
dataframe18=subset(dataframe18,uid>1800)
dataframe19=data.frame(data19)
dataframe19=subset(dataframe19,uid>1900)

dataframe2=rbind(data.frame(data2),dataframe16)
dataframe3=rbind(data.frame(data3),dataframe17)
dataframe4=rbind(data.frame(data4),dataframe18)
dataframe5=rbind(data.frame(data5),dataframe19)

#Properly assign parameters to different experiment types
dataframe2$version=1
dataframe2$Bcorrinc=55
dataframe2$Bfailinc=40


dataframe3$version=2
dataframe3$Bcorrinc=52
dataframe3$Bfailinc=40


dataframe4$version=3
dataframe4$Bcorrinc=52
dataframe4$Bfailinc=30


dataframe5$version=4
dataframe5$Bcorrinc=55
dataframe5$Bfailinc=30

#Combine data and assign useful dummies
dataframe=rbind(dataframe2,dataframe3,dataframe4,dataframe5)
dataframe$Bresponse=(dataframe$response==19)
dataframe$Aresponse=(dataframe$response==18)
dataframe$Cresponse=(dataframe$response==22)
dataframe$Bstate=(dataframe$state==4)
dataframe$Astate=(dataframe$state==3)

dataframe$Cpresent=(dataframe$qtype==4)

pa_2=sum(dataframe$Aresponse*dataframe$Bstate*dataframe$Cpresent)/sum(dataframe$Bstate*dataframe$Cpresent)

pa_1=sum(dataframe$Aresponse*dataframe$Astate*dataframe$Cpresent)/sum(dataframe$Astate*dataframe$Cpresent)


#Show N for each experiment type
#**
length(unique(dataframe2$uid))
length(unique(dataframe3$uid))
length(unique(dataframe4$uid))
length(unique(dataframe5$uid))
#**

#Now to check the weird NIAS condition.

#note the following
#b2=dataframe$Bcorrinc
#b1=dataframe$Bfailinc


#treatment 1
b2=55
b1=40



dataframe_1=subset(dataframe,(version==1))
uidlist_1=unique(dataframe_1$uid)
N=1000
n=length(uidlist_1)
randomdraw=sample(1:n,n*N,replace=TRUE)

ineqframe_1=data.frame(NIASnoC=vector('numeric'),NIASwithC_AB=vector('numeric'),NIASwithC_AC=vector('numeric'),NIASwithC_BA=vector('numeric'),NIASwithC_BC=vector('numeric'),NIASwithC_CA=vector('numeric'),NIASwithC_CB=vector('numeric'),NIAC=vector('numeric'))

for(k in 1:N){
bootframe=data.frame()
for(l in 1:n){
addframe=subset(dataframe_1,uidlist_1==uidlist_1[randomdraw[(k-1)*n+l]])
bootframe=rbind(bootframe,addframe)
}
bootframeC=subset(bootframe,Cpresent==TRUE)
bootframenoC=subset(bootframe,Cpresent==FALSE)
pAgiven1noC=sum(bootframenoC$Aresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pAgiven2noC=sum(bootframenoC$Aresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)

pAgiven1Cpres=sum(bootframeC$Aresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pAgiven2Cpres=sum(bootframeC$Aresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pBgiven1Cpres=sum(bootframeC$Bresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pBgiven2Cpres=sum(bootframeC$Bresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pCgiven1Cpres=sum(bootframeC$Cresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pCgiven2Cpres=sum(bootframeC$Cresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)

NIASnoCval=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
NIASwithC_ABval=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
NIASwithC_ACval=pAgiven2Cpres- pAgiven1Cpres
NIASwithC_BAval=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
NIASwithC_BCval=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
NIASwithC_CAval=pCgiven1Cpres-pCgiven2Cpres
NIASwithC_CBval=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
NIACval=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)


addframe=data.frame(NIASnoC=NIASnoCval,NIASwithC_AB=NIASwithC_ABval,NIASwithC_AC=NIASwithC_ACval,NIASwithC_BA=NIASwithC_BAval,NIASwithC_BC=NIASwithC_BCval,NIASwithC_CA=NIASwithC_CAval,NIASwithC_CB=NIASwithC_CBval,NIAC=NIACval)

ineqframe_1=rbind(ineqframe_1,addframe)
}
dataframe_1_C=subset(dataframe_1,Cpresent==TRUE)
dataframe_1_noC=subset(dataframe_1,Cpresent==FALSE)

pAgiven1noC=sum(dataframe_1_noC$Aresponse*dataframe_1_noC$Astate)/sum(dataframe_1_noC$Astate)
pAgiven2noC=sum(dataframe_1_noC$Aresponse*dataframe_1_noC$Bstate)/sum(dataframe_1_noC$Bstate)

pAgiven1Cpres=sum(dataframe_1_C$Aresponse*dataframe_1_C$Astate)/sum(dataframe_1_C$Astate)
pAgiven2Cpres=sum(dataframe_1_C$Aresponse*dataframe_1_C$Bstate)/sum(dataframe_1_C$Bstate)
pBgiven1Cpres=sum(dataframe_1_C$Bresponse*dataframe_1_C$Astate)/sum(dataframe_1_C$Astate)
pBgiven2Cpres=sum(dataframe_1_C$Bresponse*dataframe_1_C$Bstate)/sum(dataframe_1_C$Bstate)
pCgiven1Cpres=sum(dataframe_1_C$Cresponse*dataframe_1_C$Astate)/sum(dataframe_1_C$Astate)
pCgiven2Cpres=sum(dataframe_1_C$Cresponse*dataframe_1_C$Bstate)/sum(dataframe_1_C$Bstate)

pointNIAS_noC=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
pointNIAS_C_AB=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
pointNIAS_C_AC=pAgiven2Cpres- pAgiven1Cpres
pointNIAS_C_BA=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
pointNIAS_C_BC=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
pointNIAS_C_CA=pCgiven1Cpres-pCgiven2Cpres
pointNIAS_C_CB=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
pointNIAC=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)


points_1=c(pointNIAS_noC,pointNIAS_C_AB,pointNIAS_C_AC,pointNIAS_C_BA,pointNIAS_C_BC,pointNIAS_C_CA,pointNIAS_C_CB,pointNIAC)

sortNIASnoC=sort(ineqframe_1$NIASnoC)
i=1
while((sortNIASnoC[i]<0)&(sortNIASnoC[length(sortNIASnoC)+1-i]>0)){
i=i+1
}
pvalNIAS_noC=2*i/N

sortNIASwithC_AB=sort(ineqframe_1$NIASwithC_AB)
i=1
while((sortNIASwithC_AB[i]<0)&(sortNIASwithC_AB[length(sortNIASwithC_AB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AB=2*i/N

sortNIASwithC_AC=sort(ineqframe_1$NIASwithC_AC)
i=1
while((sortNIASwithC_AC[i]<0)&(sortNIASwithC_AC[length(sortNIASwithC_AC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AC=2*i/N

sortNIASwithC_BA=sort(ineqframe_1$NIASwithC_BA)
i=1
while((sortNIASwithC_BA[i]<0)&(sortNIASwithC_BA[length(sortNIASwithC_BA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BA=2*i/N

sortNIASwithC_BC=sort(ineqframe_1$NIASwithC_BC)
i=1
while((sortNIASwithC_BC[i]<0)&(sortNIASwithC_BC[length(sortNIASwithC_BC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BC=2*i/N

sortNIASwithC_CA=sort(ineqframe_1$NIASwithC_CA)
i=1
while((sortNIASwithC_CA[i]<0)&(sortNIASwithC_CA[length(sortNIASwithC_CA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CA=2*i/N

sortNIASwithC_CB=sort(ineqframe_1$NIASwithC_CB)
i=1
while((sortNIASwithC_CB[i]<0)&(sortNIASwithC_CB[length(sortNIASwithC_CB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CB=2*i/N

sortNIAC=sort(ineqframe_1$NIAC)
i=1
while((sortNIAC[i]<0)&(sortNIAC[length(sortNIAC)+1-i]>0)){
i=i+1
}
pvalNIAC=2*i/N

pvals_1=c(pvalNIAS_noC,pvalNIAS_C_AB,pvalNIAS_C_AC,pvalNIAS_C_BA,pvalNIAS_C_BC,pvalNIAS_C_CA,pvalNIAS_C_CB,pvalNIAC)

#treatment 2
b2=52
b1=40



dataframe_2=subset(dataframe,(version==2))
uidlist_2=unique(dataframe_2$uid)
N=1000
n=length(uidlist_2)
randomdraw=sample(1:n,n*N,replace=TRUE)

ineqframe_2=data.frame(NIASnoC=vector('numeric'),NIASwithC_AB=vector('numeric'),NIASwithC_AC=vector('numeric'),NIASwithC_BA=vector('numeric'),NIASwithC_BC=vector('numeric'),NIASwithC_CA=vector('numeric'),NIASwithC_CB=vector('numeric'),NIAC=vector('numeric'))

for(k in 1:N){
bootframe=data.frame()
for(l in 1:n){
addframe=subset(dataframe_2,uidlist_2==uidlist_2[randomdraw[(k-1)*n+l]])
bootframe=rbind(bootframe,addframe)
}
bootframeC=subset(bootframe,Cpresent==TRUE)
bootframenoC=subset(bootframe,Cpresent==FALSE)
pAgiven1noC=sum(bootframenoC$Aresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pAgiven2noC=sum(bootframenoC$Aresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)

pAgiven1Cpres=sum(bootframeC$Aresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pAgiven2Cpres=sum(bootframeC$Aresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pBgiven1Cpres=sum(bootframeC$Bresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pBgiven2Cpres=sum(bootframeC$Bresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pCgiven1Cpres=sum(bootframeC$Cresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pCgiven2Cpres=sum(bootframeC$Cresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)


NIASnoCval=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
NIASwithC_ABval=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
NIASwithC_ACval=pAgiven2Cpres- pAgiven1Cpres
NIASwithC_BAval=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
NIASwithC_BCval=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
NIASwithC_CAval=pCgiven1Cpres-pCgiven2Cpres
NIASwithC_CBval=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
NIACval=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)
addframe=data.frame(NIASnoC=NIASnoCval,NIASwithC_AB=NIASwithC_ABval,NIASwithC_AC=NIASwithC_ACval,NIASwithC_BA=NIASwithC_BAval,NIASwithC_BC=NIASwithC_BCval,NIASwithC_CA=NIASwithC_CAval,NIASwithC_CB=NIASwithC_CBval,NIAC=NIACval)

ineqframe_2=rbind(ineqframe_2,addframe)
}
dataframe_2_C=subset(dataframe_2,Cpresent==TRUE)
dataframe_2_noC=subset(dataframe_2,Cpresent==FALSE)

pAgiven1noC=sum(dataframe_2_noC$Aresponse*dataframe_2_noC$Astate)/sum(dataframe_2_noC$Astate)
pAgiven2noC=sum(dataframe_2_noC$Aresponse*dataframe_2_noC$Bstate)/sum(dataframe_2_noC$Bstate)

pAgiven1Cpres=sum(dataframe_2_C$Aresponse*dataframe_2_C$Astate)/sum(dataframe_2_C$Astate)
pAgiven2Cpres=sum(dataframe_2_C$Aresponse*dataframe_2_C$Bstate)/sum(dataframe_2_C$Bstate)
pBgiven1Cpres=sum(dataframe_2_C$Bresponse*dataframe_2_C$Astate)/sum(dataframe_2_C$Astate)
pBgiven2Cpres=sum(dataframe_2_C$Bresponse*dataframe_2_C$Bstate)/sum(dataframe_2_C$Bstate)
pCgiven1Cpres=sum(dataframe_2_C$Cresponse*dataframe_2_C$Astate)/sum(dataframe_2_C$Astate)
pCgiven2Cpres=sum(dataframe_2_C$Cresponse*dataframe_2_C$Bstate)/sum(dataframe_2_C$Bstate)

pointNIAS_noC=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
pointNIAS_C_AB=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
pointNIAS_C_AC=pAgiven2Cpres- pAgiven1Cpres
pointNIAS_C_BA=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
pointNIAS_C_BC=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
pointNIAS_C_CA=pCgiven1Cpres-pCgiven2Cpres
pointNIAS_C_CB=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
pointNIAC=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)


points_2=c(pointNIAS_noC,pointNIAS_C_AB,pointNIAS_C_AC,pointNIAS_C_BA,pointNIAS_C_BC,pointNIAS_C_CA,pointNIAS_C_CB,pointNIAC)

sortNIASnoC=sort(ineqframe_2$NIASnoC)
i=1
while((sortNIASnoC[i]<0)&(sortNIASnoC[length(sortNIASnoC)+1-i]>0)){
i=i+1
}
pvalNIAS_noC=2*i/N

sortNIASwithC_AB=sort(ineqframe_2$NIASwithC_AB)
i=1
while((sortNIASwithC_AB[i]<0)&(sortNIASwithC_AB[length(sortNIASwithC_AB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AB=2*i/N

sortNIASwithC_AC=sort(ineqframe_2$NIASwithC_AC)
i=1
while((sortNIASwithC_AC[i]<0)&(sortNIASwithC_AC[length(sortNIASwithC_AC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AC=2*i/N

sortNIASwithC_BA=sort(ineqframe_2$NIASwithC_BA)
i=1
while((sortNIASwithC_BA[i]<0)&(sortNIASwithC_BA[length(sortNIASwithC_BA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BA=2*i/N

sortNIASwithC_BC=sort(ineqframe_2$NIASwithC_BC)
i=1
while((sortNIASwithC_BC[i]<0)&(sortNIASwithC_BC[length(sortNIASwithC_BC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BC=2*i/N

sortNIASwithC_CA=sort(ineqframe_2$NIASwithC_CA)
i=1
while((sortNIASwithC_CA[i]<0)&(sortNIASwithC_CA[length(sortNIASwithC_CA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CA=2*i/N

sortNIASwithC_CB=sort(ineqframe_2$NIASwithC_CB)
i=1
while((sortNIASwithC_CB[i]<0)&(sortNIASwithC_CB[length(sortNIASwithC_CB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CB=2*i/N

sortNIAC=sort(ineqframe_2$NIAC)
i=1
while((sortNIAC[i]<0)&(sortNIAC[length(sortNIAC)+1-i]>0)){
i=i+1
}
pvalNIAC=2*i/N

pvals_2=c(pvalNIAS_noC,pvalNIAS_C_AB,pvalNIAS_C_AC,pvalNIAS_C_BA,pvalNIAS_C_BC,pvalNIAS_C_CA,pvalNIAS_C_CB,pvalNIAC)


#treatment 3
b2=55
b1=30



dataframe_3=subset(dataframe,(version==3))
uidlist_3=unique(dataframe_3$uid)
N=1000
n=length(uidlist_3)
randomdraw=sample(1:n,n*N,replace=TRUE)

ineqframe_3=data.frame(NIASnoC=vector('numeric'),NIASwithC_AB=vector('numeric'),NIASwithC_AC=vector('numeric'),NIASwithC_BA=vector('numeric'),NIASwithC_BC=vector('numeric'),NIASwithC_CA=vector('numeric'),NIASwithC_CB=vector('numeric'),NIAC=vector('numeric'))

for(k in 1:N){
bootframe=data.frame()
for(l in 1:n){
addframe=subset(dataframe_3,uidlist_3==uidlist_3[randomdraw[(k-1)*n+l]])
bootframe=rbind(bootframe,addframe)
}
bootframeC=subset(bootframe,Cpresent==TRUE)
bootframenoC=subset(bootframe,Cpresent==FALSE)
pAgiven1noC=sum(bootframenoC$Aresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pAgiven2noC=sum(bootframenoC$Aresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)

pAgiven1Cpres=sum(bootframeC$Aresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pAgiven2Cpres=sum(bootframeC$Aresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pBgiven1Cpres=sum(bootframeC$Bresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pBgiven2Cpres=sum(bootframeC$Bresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pCgiven1Cpres=sum(bootframeC$Cresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pCgiven2Cpres=sum(bootframeC$Cresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)

NIASnoCval=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
NIASwithC_ABval=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
NIASwithC_ACval=pAgiven2Cpres- pAgiven1Cpres
NIASwithC_BAval=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
NIASwithC_BCval=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
NIASwithC_CAval=pCgiven1Cpres-pCgiven2Cpres
NIASwithC_CBval=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
NIACval=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)
addframe=data.frame(NIASnoC=NIASnoCval,NIASwithC_AB=NIASwithC_ABval,NIASwithC_AC=NIASwithC_ACval,NIASwithC_BA=NIASwithC_BAval,NIASwithC_BC=NIASwithC_BCval,NIASwithC_CA=NIASwithC_CAval,NIASwithC_CB=NIASwithC_CBval,NIAC=NIACval)

ineqframe_3=rbind(ineqframe_3,addframe)
}
dataframe_3_C=subset(dataframe_3,Cpresent==TRUE)
dataframe_3_noC=subset(dataframe_3,Cpresent==FALSE)

pAgiven1noC=sum(dataframe_3_noC$Aresponse*dataframe_3_noC$Astate)/sum(dataframe_3_noC$Astate)
pAgiven2noC=sum(dataframe_3_noC$Aresponse*dataframe_3_noC$Bstate)/sum(dataframe_3_noC$Bstate)

pAgiven1Cpres=sum(dataframe_3_C$Aresponse*dataframe_3_C$Astate)/sum(dataframe_3_C$Astate)
pAgiven2Cpres=sum(dataframe_3_C$Aresponse*dataframe_3_C$Bstate)/sum(dataframe_3_C$Bstate)
pBgiven1Cpres=sum(dataframe_3_C$Bresponse*dataframe_3_C$Astate)/sum(dataframe_3_C$Astate)
pBgiven2Cpres=sum(dataframe_3_C$Bresponse*dataframe_3_C$Bstate)/sum(dataframe_3_C$Bstate)
pCgiven1Cpres=sum(dataframe_3_C$Cresponse*dataframe_3_C$Astate)/sum(dataframe_3_C$Astate)
pCgiven2Cpres=sum(dataframe_3_C$Cresponse*dataframe_3_C$Bstate)/sum(dataframe_3_C$Bstate)

pointNIAS_noC=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
pointNIAS_C_AB=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
pointNIAS_C_AC=pAgiven2Cpres- pAgiven1Cpres
pointNIAS_C_BA=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
pointNIAS_C_BC=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
pointNIAS_C_CA=pCgiven1Cpres-pCgiven2Cpres
pointNIAS_C_CB=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
pointNIAC=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)


points_3=c(pointNIAS_noC,pointNIAS_C_AB,pointNIAS_C_AC,pointNIAS_C_BA,pointNIAS_C_BC,pointNIAS_C_CA,pointNIAS_C_CB,pointNIAC)

sortNIASnoC=sort(ineqframe_3$NIASnoC)
i=1
while((sortNIASnoC[i]<0)&(sortNIASnoC[length(sortNIASnoC)+1-i]>0)){
i=i+1
}
pvalNIAS_noC=2*i/N

sortNIASwithC_AB=sort(ineqframe_3$NIASwithC_AB)
i=1
while((sortNIASwithC_AB[i]<0)&(sortNIASwithC_AB[length(sortNIASwithC_AB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AB=2*i/N

sortNIASwithC_AC=sort(ineqframe_3$NIASwithC_AC)
i=1
while((sortNIASwithC_AC[i]<0)&(sortNIASwithC_AC[length(sortNIASwithC_AC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AC=2*i/N

sortNIASwithC_BA=sort(ineqframe_3$NIASwithC_BA)
i=1
while((sortNIASwithC_BA[i]<0)&(sortNIASwithC_BA[length(sortNIASwithC_BA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BA=2*i/N

sortNIASwithC_BC=sort(ineqframe_3$NIASwithC_BC)
i=1
while((sortNIASwithC_BC[i]<0)&(sortNIASwithC_BC[length(sortNIASwithC_BC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BC=2*i/N

sortNIASwithC_CA=sort(ineqframe_3$NIASwithC_CA)
i=1
while((sortNIASwithC_CA[i]<0)&(sortNIASwithC_CA[length(sortNIASwithC_CA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CA=2*i/N

sortNIASwithC_CB=sort(ineqframe_3$NIASwithC_CB)
i=1
while((sortNIASwithC_CB[i]<0)&(sortNIASwithC_CB[length(sortNIASwithC_CB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CB=2*i/N

sortNIAC=sort(ineqframe_3$NIAC)
i=1
while((sortNIAC[i]<0)&(sortNIAC[length(sortNIAC)+1-i]>0)){
i=i+1
}
pvalNIAC=2*i/N

pvals_3=c(pvalNIAS_noC,pvalNIAS_C_AB,pvalNIAS_C_AC,pvalNIAS_C_BA,pvalNIAS_C_BC,pvalNIAS_C_CA,pvalNIAS_C_CB,pvalNIAC)


#treatment 4
b2=52
b1=30



dataframe_4=subset(dataframe,(version==4))
uidlist_4=unique(dataframe_4$uid)
N=1000
n=length(uidlist_4)
randomdraw=sample(1:n,n*N,replace=TRUE)

ineqframe_4=data.frame(NIASnoC=vector('numeric'),NIASwithC_AB=vector('numeric'),NIASwithC_AC=vector('numeric'),NIASwithC_BA=vector('numeric'),NIASwithC_BC=vector('numeric'),NIASwithC_CA=vector('numeric'),NIASwithC_CB=vector('numeric'),NIAC=vector('numeric'))

for(k in 1:N){
bootframe=data.frame()
for(l in 1:n){
addframe=subset(dataframe_4,uidlist_4==uidlist_4[randomdraw[(k-1)*n+l]])
bootframe=rbind(bootframe,addframe)
}
bootframeC=subset(bootframe,Cpresent==TRUE)
bootframenoC=subset(bootframe,Cpresent==FALSE)
pAgiven1noC=sum(bootframenoC$Aresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pAgiven2noC=sum(bootframenoC$Aresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)

pAgiven1Cpres=sum(bootframeC$Aresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pAgiven2Cpres=sum(bootframeC$Aresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pBgiven1Cpres=sum(bootframeC$Bresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pBgiven2Cpres=sum(bootframeC$Bresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pCgiven1Cpres=sum(bootframeC$Cresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pCgiven2Cpres=sum(bootframeC$Cresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)

NIASnoCval=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
NIASwithC_ABval=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
NIASwithC_ACval=pAgiven2Cpres- pAgiven1Cpres
NIASwithC_BAval=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
NIASwithC_BCval=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
NIASwithC_CAval=pCgiven1Cpres-pCgiven2Cpres
NIASwithC_CBval=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
NIACval=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)
addframe=data.frame(NIASnoC=NIASnoCval,NIASwithC_AB=NIASwithC_ABval,NIASwithC_AC=NIASwithC_ACval,NIASwithC_BA=NIASwithC_BAval,NIASwithC_BC=NIASwithC_BCval,NIASwithC_CA=NIASwithC_CAval,NIASwithC_CB=NIASwithC_CBval,NIAC=NIACval)

ineqframe_4=rbind(ineqframe_4,addframe)
}
dataframe_4_C=subset(dataframe_4,Cpresent==TRUE)
dataframe_4_noC=subset(dataframe_4,Cpresent==FALSE)

pAgiven1noC=sum(dataframe_4_noC$Aresponse*dataframe_4_noC$Astate)/sum(dataframe_4_noC$Astate)
pAgiven2noC=sum(dataframe_4_noC$Aresponse*dataframe_4_noC$Bstate)/sum(dataframe_4_noC$Bstate)

pAgiven1Cpres=sum(dataframe_4_C$Aresponse*dataframe_4_C$Astate)/sum(dataframe_4_C$Astate)
pAgiven2Cpres=sum(dataframe_4_C$Aresponse*dataframe_4_C$Bstate)/sum(dataframe_4_C$Bstate)
pBgiven1Cpres=sum(dataframe_4_C$Bresponse*dataframe_4_C$Astate)/sum(dataframe_4_C$Astate)
pBgiven2Cpres=sum(dataframe_4_C$Bresponse*dataframe_4_C$Bstate)/sum(dataframe_4_C$Bstate)
pCgiven1Cpres=sum(dataframe_4_C$Cresponse*dataframe_4_C$Astate)/sum(dataframe_4_C$Astate)
pCgiven2Cpres=sum(dataframe_4_C$Cresponse*dataframe_4_C$Bstate)/sum(dataframe_4_C$Bstate)

pointNIAS_noC=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
pointNIAS_C_AB=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
pointNIAS_C_AC=pAgiven2Cpres- pAgiven1Cpres
pointNIAS_C_BA=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
pointNIAS_C_BC=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
pointNIAS_C_CA=pCgiven1Cpres-pCgiven2Cpres
pointNIAS_C_CB=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
pointNIAC=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)


points_4=c(pointNIAS_noC,pointNIAS_C_AB,pointNIAS_C_AC,pointNIAS_C_BA,pointNIAS_C_BC,pointNIAS_C_CA,pointNIAS_C_CB,pointNIAC)

sortNIASnoC=sort(ineqframe_4$NIASnoC)
i=1
while((sortNIASnoC[i]<0)&(sortNIASnoC[length(sortNIASnoC)+1-i]>0)){
i=i+1
}
pvalNIAS_noC=2*i/N

sortNIASwithC_AB=sort(ineqframe_4$NIASwithC_AB)
i=1
while((sortNIASwithC_AB[i]<0)&(sortNIASwithC_AB[length(sortNIASwithC_AB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AB=2*i/N

sortNIASwithC_AC=sort(ineqframe_4$NIASwithC_AC)
i=1
while((sortNIASwithC_AC[i]<0)&(sortNIASwithC_AC[length(sortNIASwithC_AC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AC=2*i/N

sortNIASwithC_BA=sort(ineqframe_4$NIASwithC_BA)
i=1
while((sortNIASwithC_BA[i]<0)&(sortNIASwithC_BA[length(sortNIASwithC_BA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BA=2*i/N

sortNIASwithC_BC=sort(ineqframe_4$NIASwithC_BC)
i=1
while((sortNIASwithC_BC[i]<0)&(sortNIASwithC_BC[length(sortNIASwithC_BC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BC=2*i/N

sortNIASwithC_CA=sort(ineqframe_4$NIASwithC_CA)
i=1
while((sortNIASwithC_CA[i]<0)&(sortNIASwithC_CA[length(sortNIASwithC_CA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CA=2*i/N

sortNIASwithC_CB=sort(ineqframe_4$NIASwithC_CB)
i=1
while((sortNIASwithC_CB[i]<0)&(sortNIASwithC_CB[length(sortNIASwithC_CB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CB=2*i/N

sortNIAC=sort(ineqframe_4$NIAC)
i=1
while((sortNIAC[i]<0)&(sortNIAC[length(sortNIAC)+1-i]>0)){
i=i+1
}
pvalNIAC=2*i/N



pvals_4=c(pvalNIAS_noC,pvalNIAS_C_AB,pvalNIAS_C_AC,pvalNIAS_C_BA,pvalNIAS_C_BC,pvalNIAS_C_CA,pvalNIAS_C_CB,pvalNIAC)


NIASNIACframe_1=data.frame(Ver_1_LHS=points_1,Ver_1_pval=pvals_1)
rownames(NIASNIACframe_1)=c("NIAS no C", "NIAS w/C AB", "NIAS w/C AC","NIAS w/C BA", "NIAS w/C BC", "NIAS w/C CA", "NIAS w/C CB","NIAC")

NIASNIACframe_2=data.frame(Ver_2_LHS=points_2,Ver_2_pval=pvals_2)
rownames(NIASNIACframe_2)=c("NIAS no C", "NIAS w/C AB", "NIAS w/C AC","NIAS w/C BA", "NIAS w/C BC", "NIAS w/C CA", "NIAS w/C CB","NIAC")

NIASNIACframe_3=data.frame(Ver_3_LHS=points_3,Ver_3_pval=pvals_3)
rownames(NIASNIACframe_3)=c("NIAS no C", "NIAS w/C AB", "NIAS w/C AC","NIAS w/C BA", "NIAS w/C BC", "NIAS w/C CA", "NIAS w/C CB","NIAC")

NIASNIACframe_4=data.frame(Ver_4_LHS=points_4,Ver_4_pval=pvals_4)
rownames(NIASNIACframe_4)=c("NIAS no C", "NIAS w/C AB", "NIAS w/C AC","NIAS w/C BA", "NIAS w/C BC", "NIAS w/C CA", "NIAS w/C CB","NIAC")


#******************
stargazer(NIASNIACframe_1,summary=FALSE)
#******************

#******************
stargazer(NIASNIACframe_2,summary=FALSE)
#******************

#******************
stargazer(NIASNIACframe_3,summary=FALSE)
#******************

#******************
stargazer(NIASNIACframe_4,summary=FALSE)
#******************

#Now for individual level analysis
uidlist=unique(dataframe$uid)
#number of repetitions
N=100
#number of observations per person/block pair
n=75
#number of blocks per session
bn=2
#number of people
nuid=length(uidlist)
randomdraw=sample(1:n,nuid*bn*n*N,replace=TRUE)

indiNIASNIACframe=data.frame(uid=vector('numeric'),treatment=vector('numeric'),NIASnoC=vector('numeric'),NIASwC_AB=vector('numeric'),NIASwC_AC=vector('numeric'),NIASwC_BA=vector('numeric'),NIASwC_BC=vector('numeric'),NIASwC_CA=vector('numeric'),NIASwC_CB=vector('numeric'),NIAC=vector('numeric'),NIASnoCp=vector('numeric'),NIASwC_ABp=vector('numeric'),NIASwC_ACp=vector('numeric'),NIASwC_BAp=vector('numeric'),NIASwC_BCp=vector('numeric'),NIASwC_CAp=vector('numeric'),NIASwC_CBp=vector('numeric'),NIACp=vector('numeric'))
for(i in 1:length(uidlist)){
playframe=subset(dataframe,uid==uidlist[i])
playframeC=subset(playframe,Cpresent==TRUE)
playframenoC=subset(playframe,Cpresent==FALSE)

treatment=playframe$version[1]
b1=40*(treatment==1)+40*(treatment==2)+30*(treatment==3)+30*(treatment==4)
b2=55*(treatment==1)+52*(treatment==2)+55*(treatment==3)+52*(treatment==4)

ineqframe=data.frame(NIASnoC=vector('numeric'),NIASwithC_AB=vector('numeric'),NIASwithC_AC=vector('numeric'),NIASwithC_BA=vector('numeric'),NIASwithC_BC=vector('numeric'),NIASwithC_CA=vector('numeric'),NIASwithC_CB=vector('numeric'),NIAC=vector('numeric'))

for(j in 1:N){

bootframeC=data.frame()
bootframenoC=data.frame()
for(k in 1:n){
addframeC=playframeC[randomdraw[N*bn*n*(i-1)+bn*n*(j-1)+k],]
addframenoC=playframeC[randomdraw[N*bn*n*(i-1)+bn*n*(j-1)+n+k],]
bootframeC=rbind(bootframeC,addframeC)
bootframenoC=rbind(bootframenoC,addframenoC)
}


#trymodel=multinom(response~Bstate,data=bootframeC)


pAgiven1noC=sum(bootframenoC$Aresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pAgiven2noC=sum(bootframenoC$Aresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)

pAgiven1Cpres=sum(bootframeC$Aresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pAgiven2Cpres=sum(bootframeC$Aresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pBgiven1Cpres=sum(bootframeC$Bresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pBgiven2Cpres=sum(bootframeC$Bresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pCgiven1Cpres=sum(bootframeC$Cresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pCgiven2Cpres=sum(bootframeC$Cresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)

NIASnoCval=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
NIASwithC_ABval=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
NIASwithC_ACval=pAgiven2Cpres- pAgiven1Cpres
NIASwithC_BAval=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
NIASwithC_BCval=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
NIASwithC_CAval=pCgiven1Cpres-pCgiven2Cpres
NIASwithC_CBval=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
NIACval=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)
addframe=data.frame(NIASnoC=NIASnoCval,NIASwithC_AB=NIASwithC_ABval,NIASwithC_AC=NIASwithC_ACval,NIASwithC_BA=NIASwithC_BAval,NIASwithC_BC=NIASwithC_BCval,NIASwithC_CA=NIASwithC_CAval,NIASwithC_CB=NIASwithC_CBval,NIAC=NIACval)

ineqframe=rbind(ineqframe,addframe)

}


pAgiven1noC=sum(playframenoC$Aresponse*playframenoC$Astate)/sum(playframenoC$Astate)
pAgiven2noC=sum(playframenoC$Aresponse*playframenoC$Bstate)/sum(playframenoC$Bstate)

pAgiven1Cpres=sum(playframeC$Aresponse*playframeC$Astate)/sum(playframeC$Astate)
pAgiven2Cpres=sum(playframeC$Aresponse*playframeC$Bstate)/sum(playframeC$Bstate)
pBgiven1Cpres=sum(playframeC$Bresponse*playframeC$Astate)/sum(playframeC$Astate)
pBgiven2Cpres=sum(playframeC$Bresponse*playframeC$Bstate)/sum(playframeC$Bstate)
pCgiven1Cpres=sum(playframeC$Cresponse*playframeC$Astate)/sum(playframeC$Astate)
pCgiven2Cpres=sum(playframeC$Cresponse*playframeC$Bstate)/sum(playframeC$Bstate)

pointNIAS_noC=pAgiven1noC*(50-b1)+pAgiven2noC*(50-b2)-100+b1+b2
pointNIAS_C_AB=pAgiven1Cpres*(50-b1)+pAgiven2Cpres*(50-b2)
pointNIAS_C_AC=pAgiven2Cpres- pAgiven1Cpres
pointNIAS_C_BA=pBgiven1Cpres*(b1-50)+pBgiven2Cpres*(b2-50)
pointNIAS_C_BC=pBgiven1Cpres*(b1-100)+pBgiven2Cpres*(b2)
pointNIAS_C_CA=pCgiven1Cpres-pCgiven2Cpres
pointNIAS_C_CB=pCgiven1Cpres*(100-b1)+pCgiven2Cpres*(-b2)
pointNIAC=(pCgiven1Cpres-pCgiven2Cpres)-(pAgiven1noC-pAgiven2noC)

sortNIASnoC=sort(ineqframe$NIASnoC)
i=1
while((sortNIASnoC[i]<0)&(sortNIASnoC[length(sortNIASnoC)+1-i]>0)){
i=i+1
}
pvalNIAS_noC=2*i/N

sortNIASwithC_AB=sort(ineqframe$NIASwithC_AB)
i=1
while((sortNIASwithC_AB[i]<0)&(sortNIASwithC_AB[length(sortNIASwithC_AB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AB=2*i/N

sortNIASwithC_AC=sort(ineqframe$NIASwithC_AC)
i=1
while((sortNIASwithC_AC[i]<0)&(sortNIASwithC_AC[length(sortNIASwithC_AC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AC=2*i/N

sortNIASwithC_BA=sort(ineqframe$NIASwithC_BA)
i=1
while((sortNIASwithC_BA[i]<0)&(sortNIASwithC_BA[length(sortNIASwithC_BA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BA=2*i/N

sortNIASwithC_BC=sort(ineqframe$NIASwithC_BC)
i=1
while((sortNIASwithC_BC[i]<0)&(sortNIASwithC_BC[length(sortNIASwithC_BC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BC=2*i/N

sortNIASwithC_CA=sort(ineqframe$NIASwithC_CA)
i=1
while((sortNIASwithC_CA[i]<0)&(sortNIASwithC_CA[length(sortNIASwithC_CA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CA=2*i/N

sortNIASwithC_CB=sort(ineqframe$NIASwithC_CB)
i=1
while((sortNIASwithC_CB[i]<0)&(sortNIASwithC_CB[length(sortNIASwithC_CB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CB=2*i/N

sortNIAC=sort(ineqframe$NIAC)
i=1
while((sortNIAC[i]<0)&(sortNIAC[length(sortNIAC)+1-i]>0)){
i=i+1
}
pvalNIAC=2*i/N

#pvals=c(pvalNIAS_noC,pvalNIAS_C_AB,pvalNIAS_C_AC,pvalNIAS_C_BA,pvalNIAS_C_BC,pvalNIAS_C_CA,pvalNIAS_C_CB,pvalNIAC)

#points=c(pointNIAS_noC,pointNIAS_C_AB,pointNIAS_C_AC,pointNIAS_C_BA,pointNIAS_C_BC,pointNIAS_C_CA,pointNIAS_C_CB,pointNIAC)


addframe=data.frame(uid=playframe$uid[1],treatment=playframe$version[1],NIASnoC=pointNIAS_noC,NIASwC_AB=pointNIAS_C_AB,NIASwC_AC=pointNIAS_C_AC,NIASwC_BA=pointNIAS_C_BA, NIASwC_BC=pointNIAS_C_BC, NIASwC_CA=pointNIAS_C_CA,NIASwC_CB=pointNIAS_C_CB,NIAC=pointNIAC,NIASnoCp=pvalNIAS_noC,NIASwC_ABp=pvalNIAS_C_AB,NIASwC_ACp=pvalNIAS_C_AC,NIASwC_BAp=pvalNIAS_C_BA,NIASwC_BCp=pvalNIAS_C_BC, NIASwC_CAp=pvalNIAS_C_CA, NIASwC_CBp=pvalNIAS_C_CB, NIACp=pvalNIAC)
indiNIASNIACframe=rbind(indiNIASNIACframe,addframe)


}

indiNIASNIACframe$noCNIASviol=(indiNIASNIACframe$NIASnoC<0)*(indiNIASNIACframe$NIASnoCp<0.05)

indiNIASNIACframe$wCNIASviol=(((indiNIASNIACframe$NIASwC_AB<0)*(indiNIASNIACframe$NIASwC_ABp<0.05)+(indiNIASNIACframe$NIASwC_AC<0)*(indiNIASNIACframe$NIASwC_ACp<0.05)+(indiNIASNIACframe$NIASwC_BC<0)*(indiNIASNIACframe$NIASwC_BAp<0.05)+(indiNIASNIACframe$NIASwC_BC<0)*(indiNIASNIACframe$NIASwC_BCp<0.05)+(indiNIASNIACframe$NIASwC_CA<0)*(indiNIASNIACframe$NIASwC_CAp<0.05)+(indiNIASNIACframe$NIASwC_CB<0)*(indiNIASNIACframe$NIASwC_CBp<0.05))>0)

indiNIASNIACframe$NIACviol=(indiNIASNIACframe$NIAC<0)*(indiNIASNIACframe$NIACp<0.05)

indiNIASNIACframe1=subset(indiNIASNIACframe,treatment==1)
indiNIASNIACframe2=subset(indiNIASNIACframe,treatment==2)
indiNIASNIACframe3=subset(indiNIASNIACframe,treatment==3)
indiNIASNIACframe4=subset(indiNIASNIACframe,treatment==4)


NIASnoCviolvec=c(sum(indiNIASNIACframe1$noCNIASviol),sum(indiNIASNIACframe2$noCNIASviol),sum(indiNIASNIACframe3$noCNIASviol),sum(indiNIASNIACframe4$noCNIASviol))
NIASwCviolvec=c(sum(indiNIASNIACframe1$wCNIASviol),sum(indiNIASNIACframe2$wCNIASviol),sum(indiNIASNIACframe3$wCNIASviol),sum(indiNIASNIACframe4$wCNIASviol))
NIACviolvec=c(sum(indiNIASNIACframe1$NIACviol),sum(indiNIASNIACframe2$NIACviol),sum(indiNIASNIACframe3$NIACviol),sum(indiNIASNIACframe4$NIACviol))
totalvec=c(length(indiNIASNIACframe1$uid),length(indiNIASNIACframe2$uid),length(indiNIASNIACframe3$uid),length(indiNIASNIACframe4$uid))

NIASNIACreport=data.frame(NIASviolationsC=NIASwCviolvec,NIASviolations_noC=NIASnoCviolvec,NIACviolations=NIACviolvec,TotalPlayers=totalvec)
rownames(NIASNIACreport)=c("Treatment 1","Treatment 2","Treatment 3","Treatment 4")

#Related Test******************
stargazer(NIASNIACreport,summary=FALSE)
#*********************

NIASviolwC=c(indiNIASNIACframe1$wCNIASviol,indiNIASNIACframe2$wCNIASviol,indiNIASNIACframe3$wCNIASviol,indiNIASNIACframe4$wCNIASviol)
NIASviolnoC=c(indiNIASNIACframe1$noCNIASviol,indiNIASNIACframe2$noCNIASviol,indiNIASNIACframe3$noCNIASviol,indiNIASNIACframe4$noCNIASviol)
NIASviol=pmax(NIASviolwC,NIASviolnoC)
NIACviol=c(indiNIASNIACframe1$NIACviol,indiNIASNIACframe2$NIACviol,indiNIASNIACframe3$NIACviol,indiNIASNIACframe4$NIACviol)

NIASonly=sum(NIASviol*(1-NIACviol))
NIAConly=sum(NIACviol*(1-NIASviol))
both=sum(NIASviol*(NIACviol))
neither=sum((1-NIASviol)*(1-NIACviol))

NIASonly
NIAConly
both
neither
#Raw average numbers


bootframenoC=subset(dataframe_4,Cpresent==FALSE)
bootframeC=subset(dataframe_4,Cpresent==TRUE)

pAgiven1noC=sum(bootframenoC$Aresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pAgiven2noC=sum(bootframenoC$Aresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)
pBgiven1noC=sum(bootframenoC$Bresponse*bootframenoC$Astate)/sum(bootframenoC$Astate)
pBgiven2noC=sum(bootframenoC$Bresponse*bootframenoC$Bstate)/sum(bootframenoC$Bstate)

pAgiven1Cpres=sum(bootframeC$Aresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pAgiven2Cpres=sum(bootframeC$Aresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pBgiven1Cpres=sum(bootframeC$Bresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pBgiven2Cpres=sum(bootframeC$Bresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)
pCgiven1Cpres=sum(bootframeC$Cresponse*bootframeC$Astate)/sum(bootframeC$Astate)
pCgiven2Cpres=sum(bootframeC$Cresponse*bootframeC$Bstate)/sum(bootframeC$Bstate)


pAgiven1noC
pAgiven2noC
pBgiven1noC
pBgiven2noC

pAgiven1Cpres
pAgiven2Cpres
pBgiven1Cpres
pBgiven2Cpres
pCgiven1Cpres
pCgiven2Cpres

unique(dataframe_2$uid)




#aggregate



N=1000
n1=length(uidlist_1)
randomdraw1=sample(1:n1,n1*N,replace=TRUE)
n2=length(uidlist_2)
randomdraw2=sample(1:n2,n2*N,replace=TRUE)
n3=length(uidlist_3)
randomdraw3=sample(1:n3,n3*N,replace=TRUE)
n4=length(uidlist_4)
randomdraw4=sample(1:n4,n4*N,replace=TRUE)

dataframe$b1=40*(dataframe$version==1)+40*(dataframe$version==2)+30*(dataframe$version==3)+30*(dataframe$version==4)
dataframe$b2=55*(dataframe$version==1)+52*(dataframe$version==2)+55*(dataframe$version==3)+52*(dataframe$version==4)


ineqframe=data.frame(NIASnoC=vector('numeric'),NIASwithC_AB=vector('numeric'),NIASwithC_AC=vector('numeric'),NIASwithC_BA=vector('numeric'),NIASwithC_BC=vector('numeric'),NIASwithC_CA=vector('numeric'),NIASwithC_CB=vector('numeric'),NIAC=vector('numeric'))

for(k in 1:N){
bootframe=data.frame()
for(l in 1:n1){
addframe=subset(dataframe, uid==uidlist_1[randomdraw1[(k-1)*n1+l]])
bootframe=rbind(bootframe,addframe)
}

for(l in 1:n2){
addframe=subset(dataframe, uid==uidlist_2[randomdraw2[(k-1)*n2+l]])
bootframe=rbind(bootframe,addframe)
}

for(l in 1:n3){
addframe=subset(dataframe, uid==uidlist_3[randomdraw3[(k-1)*n3+l]])
bootframe=rbind(bootframe,addframe)
}

for(l in 1:n4){
addframe=subset(dataframe, uid==uidlist_4[randomdraw4[(k-1)*n4+l]])
bootframe=rbind(bootframe,addframe)
}
bootframe_noC=subset(bootframe,Cpresent==FALSE)
bootframe_C=subset(bootframe,Cpresent==TRUE)


bootframe_noC$Agiven1noC=(bootframe_noC$Aresponse*bootframe_noC$Astate)/(sum(bootframe_noC$Astate)/length(bootframe_noC$Astate))
bootframe_noC$Agiven2noC=(bootframe_noC$Aresponse*bootframe_noC$Bstate)/(sum(bootframe_noC$Bstate)/length(bootframe_noC$Bstate))

bootframe_C$Agiven1Cpres=(bootframe_C$Aresponse*bootframe_C$Astate)/(sum(bootframe_C$Astate)/length(bootframe_C$Astate))
bootframe_C$Agiven2Cpres=(bootframe_C$Aresponse*bootframe_C$Bstate)/(sum(bootframe_C$Bstate)/length(bootframe_C$Bstate))
bootframe_C$Bgiven1Cpres=(bootframe_C$Bresponse*bootframe_C$Astate)/(sum(bootframe_C$Astate)/length(bootframe_C$Astate))
bootframe_C$Bgiven2Cpres=(bootframe_C$Bresponse*bootframe_C$Bstate)/(sum(bootframe_C$Bstate)/length(bootframe_C$Bstate))
bootframe_C$Cgiven1Cpres=(bootframe_C$Cresponse*bootframe_C$Astate)/(sum(bootframe_C$Astate)/length(bootframe_C$Astate))
bootframe_C$Cgiven2Cpres=(bootframe_C$Cresponse*bootframe_C$Bstate)/(sum(bootframe_C$Bstate)/length(bootframe_C$Bstate))



NIASnoCval=mean(na.omit(bootframe_noC$Agiven1noC*(50-bootframe_noC$b1)))+mean(na.omit(bootframe_noC$Agiven2noC*(50-bootframe_noC$b2)))-100+mean(bootframe_noC$b1+bootframe_noC$b2)
NIASwithC_ABval=mean(na.omit(bootframe_C$Agiven1Cpres*(50-bootframe_C$b1)))+mean(na.omit(bootframe_C$Agiven2Cpres*(50-bootframe_C$b2)))
NIASwithC_ACval=mean(na.omit(bootframe_C$Agiven2Cpres))- mean(na.omit(bootframe_C$Agiven1Cpres))
NIASwithC_BAval=mean(na.omit(bootframe_C$Bgiven1Cpres*(bootframe_C$b1-50)))+mean(na.omit(bootframe_C$Bgiven2Cpres*(bootframe_C$b2-50)))
NIASwithC_BCval=mean(na.omit(bootframe_C$Bgiven1Cpres*(bootframe_C$b1-100)))+mean(na.omit(bootframe_C$Bgiven2Cpres*(bootframe_C$b2)))
NIASwithC_CAval=mean(na.omit(bootframe_C$Cgiven1Cpres))-mean(na.omit(bootframe_C$Cgiven2Cpres))
NIASwithC_CBval=mean(na.omit(bootframe_C$Cgiven1Cpres*(100-bootframe_C$b1)))+mean(na.omit(bootframe_C$Cgiven2Cpres*(-bootframe_C$b2)))
NIACval=mean(na.omit((bootframe_C$Cgiven1Cpres))-mean(na.omit(bootframe_C$Cgiven2Cpres)))-mean(na.omit((bootframe_noC$Agiven1noC))-mean(na.omit(bootframe_noC$Agiven2noC)))

addframe=data.frame(NIASnoC=NIASnoCval,NIASwithC_AB=NIASwithC_ABval,NIASwithC_AC=NIASwithC_ACval,NIASwithC_BA=NIASwithC_BAval,NIASwithC_BC=NIASwithC_BCval,NIASwithC_CA=NIASwithC_CAval,NIASwithC_CB=NIASwithC_CBval,NIAC=NIACval)

ineqframe=rbind(ineqframe,addframe)
}



dataframe_C=subset(dataframe,Cpresent==TRUE)
dataframe_noC=subset(dataframe,Cpresent==FALSE)


dataframe_noC$Agiven1noC=(dataframe_noC$Aresponse*dataframe_noC$Astate)/(sum(dataframe_noC$Astate)/length(dataframe_noC$Astate))
dataframe_noC$Agiven2noC=(dataframe_noC$Aresponse*dataframe_noC$Bstate)/(sum(dataframe_noC$Bstate)/length(dataframe_noC$Bstate))


dataframe_C$Agiven1Cpres=(dataframe_C$Aresponse*dataframe_C$Astate)/(sum(dataframe_C$Astate)/length(dataframe_C$Astate))
dataframe_C$Agiven2Cpres=(dataframe_C$Aresponse*dataframe_C$Bstate)/(sum(dataframe_C$Bstate)/length(dataframe_C$Bstate))
dataframe_C$Bgiven1Cpres=(dataframe_C$Bresponse*dataframe_C$Astate)/(sum(dataframe_C$Astate)/length(dataframe_C$Astate))
dataframe_C$Bgiven2Cpres=(dataframe_C$Bresponse*dataframe_C$Bstate)/(sum(dataframe_C$Bstate)/length(dataframe_C$Bstate))
dataframe_C$Cgiven1Cpres=(dataframe_C$Cresponse*dataframe_C$Astate)/(sum(dataframe_C$Astate)/length(dataframe_C$Astate))
dataframe_C$Cgiven2Cpres=(dataframe_C$Cresponse*dataframe_C$Bstate)/(sum(dataframe_C$Bstate)/length(dataframe_C$Bstate))

pointNIAS_noC=mean(na.omit(dataframe_noC$Agiven1noC*(50-dataframe_noC$b1)))+mean(na.omit(dataframe_noC$Agiven2noC*(50-dataframe_noC$b2)))-100+mean(dataframe_noC$b1+dataframe_noC$b2)
pointNIAS_C_AB=mean(na.omit(dataframe_C$Agiven1Cpres*(50-dataframe_C$b1)))+mean(na.omit(dataframe_C$Agiven2Cpres*(50-dataframe_C$b2)))
pointNIAS_C_AC=mean(na.omit(dataframe_C$Agiven2Cpres))- mean(na.omit(dataframe_C$Agiven1Cpres))
pointNIAS_C_BA=mean(na.omit(dataframe_C$Bgiven1Cpres*(dataframe_C$b1-50)))+mean(na.omit(dataframe_C$Bgiven2Cpres*(dataframe_C$b2-50)))
pointNIAS_C_BC=mean(na.omit(dataframe_C$Bgiven1Cpres*(dataframe_C$b1-100)))+mean(na.omit(dataframe_C$Bgiven2Cpres*(dataframe_C$b2)))
pointNIAS_C_CA=mean(na.omit(dataframe_C$Cgiven1Cpres))-mean(na.omit(dataframe_C$Cgiven2Cpres))
pointNIAS_C_CB=mean(na.omit(dataframe_C$Cgiven1Cpres*(100-dataframe_C$b1)))+mean(na.omit(dataframe_C$Cgiven2Cpres*(-dataframe_C$b2)))
pointNIAC=mean(na.omit((dataframe_C$Cgiven1Cpres))-mean(na.omit(dataframe_C$Cgiven2Cpres)))-mean(na.omit((dataframe$Agiven1noC))-mean(na.omit(dataframe$Agiven2noC)))



points=c(pointNIAS_noC,pointNIAS_C_AB,pointNIAS_C_AC,pointNIAS_C_BA,pointNIAS_C_BC,pointNIAS_C_CA,pointNIAS_C_CB,pointNIAC)

sortNIASnoC=sort(ineqframe$NIASnoC)
i=1
while((sortNIASnoC[i]<0)&(sortNIASnoC[length(sortNIASnoC)+1-i]>0)){
i=i+1
}
pvalNIAS_noC=2*i/N

sortNIASwithC_AB=sort(ineqframe$NIASwithC_AB)
i=1
while((sortNIASwithC_AB[i]<0)&(sortNIASwithC_AB[length(sortNIASwithC_AB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AB=2*i/N

sortNIASwithC_AC=sort(ineqframe$NIASwithC_AC)
i=1
while((sortNIASwithC_AC[i]<0)&(sortNIASwithC_AC[length(sortNIASwithC_AC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_AC=2*i/N

sortNIASwithC_BA=sort(ineqframe$NIASwithC_BA)
i=1
while((sortNIASwithC_BA[i]<0)&(sortNIASwithC_BA[length(sortNIASwithC_BA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BA=2*i/N

sortNIASwithC_BC=sort(ineqframe$NIASwithC_BC)
i=1
while((sortNIASwithC_BC[i]<0)&(sortNIASwithC_BC[length(sortNIASwithC_BC)+1-i]>0)){
i=i+1
}
pvalNIAS_C_BC=2*i/N

sortNIASwithC_CA=sort(ineqframe$NIASwithC_CA)
i=1
while((sortNIASwithC_CA[i]<0)&(sortNIASwithC_CA[length(sortNIASwithC_CA)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CA=2*i/N

sortNIASwithC_CB=sort(ineqframe$NIASwithC_CB)
i=1
while((sortNIASwithC_CB[i]<0)&(sortNIASwithC_CB[length(sortNIASwithC_CB)+1-i]>0)){
i=i+1
}
pvalNIAS_C_CB=2*i/N

sortNIAC=sort(ineqframe$NIAC)
i=1
while((sortNIAC[i]<0)&(sortNIAC[length(sortNIAC)+1-i]>0)){
i=i+1
}
pvalNIAC=2*i/N



pvals=c(pvalNIAS_noC,pvalNIAS_C_AB,pvalNIAS_C_AC,pvalNIAS_C_BA,pvalNIAS_C_BC,pvalNIAS_C_CA,pvalNIAS_C_CB,pvalNIAC)


NIASNIACframe_agg=data.frame(LHS=points,pval=pvals)
rownames(NIASNIACframe_agg)=c("NIAS no C", "NIAS w/C AB", "NIAS w/C AC","NIAS w/C BA", "NIAS w/C BC", "NIAS w/C CA", "NIAS w/C CB","NIAC")



#Table A1.2********************
stargazer(NIASNIACframe_agg,summary=FALSE)
#*********************